"""
    基类：存储一些公共方法
"""
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from config import BASE_DIR
from tool.get_driver import DriverUtils
import time

class BasePage:

    # 初始化：获取驱动对象
    def __init__(self):
        self.driver = DriverUtils.get_admin_driver()

    # 书写公共基类方法
    # 查找元素
    def base_find_element(self,loc):
        try:

            el = WebDriverWait(self.driver,10,1).until(lambda x:x.find_element(*loc))
            print(f"excute find_el {loc} success!")
        except Exception as e:
            print(f"excute find_el {loc} error!")
            el = None
        # 返回查找元素
        return el

    # 点击方法
    def base_click(self,loc):
            try:
                el = self.base_find_element(loc)
                el.click()
                print(f"excute base_click {loc} success!")
            except Exception as e:
                print(f"excute base_click {loc} failed")
                print()

    # 输入文本方法
    def base_input_text(self,loc,key_text):
            try:
                el = self.base_find_element(loc)
                # 消除文本的所有的文字
                el.clear()
                el.send_keys(key_text)
                print(f"excute base_input_text {loc} success!")
            except Exception as e:
                print(f"excute base_input_text {loc} failed!")


    # 切换窗口
    def base_switch_windows(self,n):
        """
        :param n:  切换第几个窗口
        :return:
        """
        try:
            # 获取所有的句柄
            handles = self.driver.window_handles
            # 切换窗口
            self.driver.switch_to.window(handles[n])
        except Exception as e:
            print(f"switch to {n} window error!")
    # 切换表单
    def base_switch_frames(self,el):
        """
        :param el: <iframe>标签的元素对象
        :return: None
        """
        try:
            # 切换表单
            self.driver.switch_to.frame(el)
        except Exception as e:
            print(f"switch to {el} window error!")
    # 截图
    def base_get_screen(self):
        self.driver.get_screenshot_as_file(BASE_DIR+"/image/{}.png".format(time.strftime("%Y%m%d_%H%M%S")))

    # 上传图片
    def base_upload(self):
        # 上传菜品图片
        self.driver.find_element(By.CSS_SELECTOR, ".avatar-uploader-icon + input[type='file']").send_keys(
            r"C:\Users\13428154264\OneDrive\图片\2.jpg")
        time.sleep(3)







